import time
import cplex
import copy as cp
import MYGRAPH_CLASS as mgc
#import itertools as it
import ILP_CLASS as ilpc
from cplex.exceptions import CplexError
import sys
import random

File_Out = open("Data_Path_Length.csv", 'w')
File_Out.write("Path Length, Soln #, Average Max Path Length, Time Per Problem" \
               + ", Average Cost"+ "\n")

ILP_Sum = 0
ILP_Soln_Num = 0
Max_Length_Sum = 0.0

random.seed()

Test_Num = 1000
Path_Length = 0
Trial_Num = 0
while Path_Length < 5:
    Start_Time = time.clock()
    ILP_Sum = 0
    ILP_Soln_Num = 0
    Trial_Num = 0
    Max_Length_Sum = 0.0
    while Trial_Num < Test_Num:
        OurGraph = mgc.MyGraph([7, 7], 10, 600)
        OurGraph.Set_Latencies()
        OurGraph.Set_DC_Owners()
        OurGraph.Set_Edge_Costs()
        OurGraph.Set_DC_Costs()
        OurGraph.Set_Max_Lats()
        
        MyILP = ilpc.Ilp(OurGraph, Path_Length)
        MyILP.Set_Objective()   
        MyILP.Add_Latency_Constraints()
        try:
            MyILP.Solve_Problem()
            MyILP.Print_Solution()
            Cost = MyILP.Get_Soln()
            print(Cost)
            print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
            Max_Length_Sum = Max_Length_Sum + MyILP.Print_Solution()
            ILP_Sum = ILP_Sum + Cost
            ILP_Soln_Num = ILP_Soln_Num + 1
        except CplexError:
            print("well that didn't work...")
        print(Trial_Num)  
        Trial_Num = Trial_Num + 1
    if ILP_Soln_Num != 0:
        ILP_Avg = ILP_Sum/ILP_Soln_Num
        Max_Length_Avg = Max_Length_Sum/ILP_Soln_Num
    else:
        ILP_Avg = 0
        Max_Length_Avg = 0
    File_Out.write(str(Path_Length) + ',')
    File_Out.write(str(ILP_Soln_Num) + ',')
    File_Out.write(str(Max_Length_Avg) + ',')
    File_Out.write(str(time.clock() - Start_Time/Test_Num) + ',')
    File_Out.write(str(ILP_Avg) + '\n')
    Path_Length = Path_Length + 1








